home *** CD-ROM | disk | FTP | other *** search
- package horst;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Hashtable;
- import java.util.zip.InflaterInputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipException;
- import java.util.zip.ZipInputStream;
-
- public class URLClassLoader extends ClassLoader {
- private Hashtable classes = new Hashtable();
- private String urlString;
- private char classNameReplacementChar;
- private boolean bReadingJar;
-
- protected String formatClassName(String className) {
- return this.classNameReplacementChar == 0 ? className.replace('.', '/') + ".class" : className.replace('.', this.classNameReplacementChar) + ".class";
- }
-
- public Class loadClass(String className) throws ClassNotFoundException {
- return this.loadClass(className, true);
- }
-
- public synchronized Class loadClass(String className, boolean resolveIt) throws ClassNotFoundException {
- Class result = (Class)this.classes.get(className);
- if (result != null) {
- return result;
- } else {
- try {
- result = super.findSystemClass(className);
- return result;
- } catch (ClassNotFoundException var6) {
- byte[] classBytes = this.loadClassBytes(className);
- if (classBytes == null) {
- throw new ClassNotFoundException();
- } else {
- try {
- result = ((ClassLoader)this).defineClass(classBytes, 0, classBytes.length);
- if (result == null) {
- throw new ClassFormatError();
- }
- } catch (Exception var5) {
- throw new ClassFormatError();
- }
-
- if (resolveIt) {
- System.out.println("resolving : " + result);
- ((ClassLoader)this).resolveClass(result);
- }
-
- this.classes.put(className, result);
- return result;
- }
- }
- }
- }
-
- protected byte[] loadClassBytes(String className) {
- className = this.formatClassName(className);
- if (this.bReadingJar) {
- return this.readJAREntry(className);
- } else {
- try {
- URL url = new URL(this.urlString + "/" + className);
- URLConnection connection = url.openConnection();
- InputStream inputStream = connection.getInputStream();
- int length = connection.getContentLength();
- byte[] data = new byte[length];
- int numRead = 0;
-
- do {
- try {
- numRead += inputStream.read(data, numRead, length - numRead);
- } catch (IOException ioe) {
- System.out.println("URLClassLoader :" + ((Throwable)ioe).getMessage());
- break;
- }
- } while(numRead < length);
-
- inputStream.close();
- return data;
- } catch (Exception ex) {
- System.out.print("### URLClassLoader.loadClassBytes() - Exception:");
- ((Throwable)ex).printStackTrace();
- return null;
- }
- }
- }
-
- public synchronized void loadJAR(String base, String jarFile) throws Exception {
- this.bReadingJar = true;
- String separator = "/";
- String jarStr = "";
- if (base.endsWith(separator)) {
- jarStr = base + jarFile;
- } else {
- jarStr = base + separator + jarFile;
- }
-
- this.setURLString(jarStr);
- URL u = Utilities.getURL(jarStr);
- if (u != null) {
- try {
- ZipInputStream is = new ZipInputStream(u.openStream());
- ZipEntry zipEntry = null;
-
- while((zipEntry = is.getNextEntry()) != null) {
- String className = zipEntry.getName();
- if (className.endsWith(".class")) {
- int len = className.length();
- className = className.substring(0, len - 6);
- Class result = (Class)this.classes.get(className);
- if (result == null) {
- try {
- super.findSystemClass(className);
- } catch (ClassNotFoundException var14) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int currByte = 0;
-
- while((currByte = ((InflaterInputStream)is).read()) >= 0) {
- out.write(currByte);
- }
-
- byte[] classBytes = out.toByteArray();
- System.out.println("className=" + className);
- result = ((ClassLoader)this).defineClass(classBytes, 0, classBytes.length);
- if (result != null) {
- this.classes.put(className, result);
- }
- }
- }
- }
- }
-
- is.close();
- } catch (ZipException var15) {
- System.out.println("ZipException!");
- } catch (IOException var16) {
- System.out.println("IOException!");
- }
- }
-
- this.bReadingJar = false;
- }
-
- byte[] readJAREntry(String className) {
- byte[] classBytes = new byte[0];
-
- try {
- URL u = Utilities.getURL(this.urlString);
- ZipInputStream is = new ZipInputStream(u.openStream());
- ZipEntry zipEntry = null;
-
- while((zipEntry = is.getNextEntry()) != null) {
- String entryName = zipEntry.getName();
- if (entryName.equalsIgnoreCase(className)) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int currByte = 0;
-
- while((currByte = ((InflaterInputStream)is).read()) >= 0) {
- out.write(currByte);
- }
-
- classBytes = out.toByteArray();
- break;
- }
- }
-
- is.close();
- } catch (ZipException var9) {
- System.out.println("readJAREntry ZipException!");
- } catch (IOException var10) {
- System.out.println("readJAREntry IOException!");
- }
-
- return classBytes;
- }
-
- public void setClassNameReplacementChar(char replacement) {
- this.classNameReplacementChar = replacement;
- }
-
- void setURLString(String url) {
- this.urlString = url;
- }
- }
-